package ro.msg.office.entities;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="TB_USER")
@SequenceGenerator(name="USER_SEQUENCE_GENERATOR", 
		    sequenceName="USER_SEQUENCE", 
		            initialValue=1, allocationSize=10)

/******************************************
   Query for:
   *findAll
   *findById
   *findByNameAndPassword
   *findTicketsByUsername
   *findById
 ******************************************/
@NamedQueries({
	@NamedQuery(name="User.findAll",query="SELECT u FROM User u"),
	@NamedQuery(name="User.findById",query="SELECT u FROM User u WHERE u.userId = :id"),
	@NamedQuery(name="User.findByNameAndPassword", query="SELECT u FROM User u WHERE u.username = :name AND u.password = :password"),
	@NamedQuery(name="User.findTicketsByUsername", query="SELECT u.tickets FROM User u WHERE u.username = :username"),
	@NamedQuery(name="User.findNameById", query="SELECT u.name FROM User u WHERE u.userId = :id")
})
public class User {

	/******************************************
	   Generate autoincrement for Id
	 ******************************************/		         
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE,
		    generator="USER_SEQUENCE_GENERATOR")
 	@Column(name="userId")
  	private Long userId;
	
	/******************************************
	   ManyToOne relationship 
	 ******************************************/
	@ManyToOne
	@JoinColumn(name = "departmentId")
	private Department department;
	
	@Column(name="username")
	private String username;
	
	@Column(name="password")
	private String password;
	
	@Column(name="name")
	private String name;
	
	@Column(name="userType")
	private Long userType;
	
	/******************************************
	   OneToMany relationship 
	 ******************************************/
	@OneToMany(cascade=CascadeType.ALL, mappedBy="user")
	//@OrderBy("ticketId")
	private Set<Ticket> tickets;
  	
	public User(){
		super();
	}
	
	public User(Long userId, Department department, String username,
			String password, String name, Long userType, Set<Ticket> tickets) {
		super();
		this.userId = userId;
		this.department = department;
		this.username = username;
		this.password = password;
		this.name = name;
		this.userType = userType;
		this.tickets = tickets;
	}

	public Long getUserId(){
		return userId;
	}
	
	public void setUserId(Long userId){
		this.userId = userId;
	}
	
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getUserType() {
		return userType;
	}

	public void setUserType(Long userType) {
		this.userType = userType;
	}

	public Set<Ticket> getTickets() {
		return tickets;
	}

	public void setTickets(Set<Ticket> tickets) {
		this.tickets = tickets;
	}
	
	public void addTicket(Ticket ticket){
		this.getTickets().add(ticket);
		ticket.setUser(this);
	}
}